﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;

    public class ServerException : Exception
    {
        private int m_serverErrorCode;
        private object m_serverErrorDetails;
        private string m_serverErrorTypeName;
        private string m_serverErrorValue;
        private string m_serverStackTrace;

        internal ServerException(string message, string serverStackTrace, int serverErrorCode, string serverErrorValue, string serverErrorTypeName, object serverErrorDetails) : base(message)
        {
            this.m_serverStackTrace = serverStackTrace;
            this.m_serverErrorCode = serverErrorCode;
            this.m_serverErrorValue = serverErrorValue;
            this.m_serverErrorTypeName = serverErrorTypeName;
            this.m_serverErrorDetails = serverErrorDetails;
        }

        internal static ServerException CreateFromErrorInfo(Dictionary<string, object> errorInfo)
        {
            object obj2;
            string str;
            string str2;
            if (errorInfo.TryGetValue("ErrorMessage", out obj2))
            {
                str = (string) obj2;
            }
            else
            {
                str = string.Empty;
            }
            if (errorInfo.TryGetValue("ErrorStackTrace", out obj2))
            {
                str2 = (string) obj2;
            }
            else
            {
                str2 = string.Empty;
            }
            int serverErrorCode = 0;
            if (errorInfo.TryGetValue("ErrorCode", out obj2))
            {
                if (obj2 is int)
                {
                    serverErrorCode = (int) obj2;
                }
                else if (obj2 is long)
                {
                    serverErrorCode = (int) ((long) obj2);
                }
            }
            string serverErrorValue = null;
            if (errorInfo.TryGetValue("ErrorValue", out obj2))
            {
                serverErrorValue = (string) obj2;
            }
            string serverErrorTypeName = null;
            if (errorInfo.TryGetValue("ErrorTypeName", out obj2))
            {
                serverErrorTypeName = (string) obj2;
            }
            object serverErrorDetails = null;
            if (errorInfo.TryGetValue("ErrorDetails", out obj2))
            {
                serverErrorDetails = obj2;
            }
            if (serverErrorCode == -2147024891)
            {
                return new ServerUnauthorizedAccessException(str, str2, serverErrorCode, serverErrorValue, serverErrorTypeName, serverErrorDetails);
            }
            return new ServerException(str, str2, serverErrorCode, serverErrorValue, serverErrorTypeName, serverErrorDetails);
        }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.ServerStackTrace))
            {
                return base.ToString();
            }
            return (this.Message + Environment.NewLine + this.ServerStackTrace);
        }

        public int ServerErrorCode
        {
            get
            {
                return this.m_serverErrorCode;
            }
        }

        public object ServerErrorDetails
        {
            get
            {
                return this.m_serverErrorDetails;
            }
        }

        public string ServerErrorTypeName
        {
            get
            {
                return this.m_serverErrorTypeName;
            }
        }

        public string ServerErrorValue
        {
            get
            {
                return this.m_serverErrorValue;
            }
        }

        public string ServerStackTrace
        {
            get
            {
                return this.m_serverStackTrace;
            }
        }
    }
}

